<template>
  <div class="userPermission-page app-container">
    <el-form ref="dataForm" :inline="true" label-width="80px" :model="dataForm">
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="dataForm.userName" clearable placeholder="请输入用户名称" style="width: 260px;"></el-input>
      </el-form-item>
      <el-form-item style="margin-left: 20px">
        <el-button class="bg-green" @click="getList">查询</el-button>
        <el-button type="danger" class="search-box" @click="updateHandle()">添加</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      border fit
      size="mini"
      :header-cell-style="{background:'#dcedff',color:'#32639d'}"
      style="width: 100%;"
    >
      <el-table-column type="index" label="序号" align="center" width="60" fixed></el-table-column>
      <el-table-column prop="username" label="用户名称" min-width="120" fixed align="center"></el-table-column>
      <el-table-column prop="roleName" label="角色" min-width="120" fixed align="center"></el-table-column>
      <el-table-column prop="userId" label="用户Id" min-width="130" align="center"></el-table-column>
      <el-table-column prop="mobile" label="手机号码" width="130" align="center"></el-table-column>
      <el-table-column prop="email" label="邮箱" min-width="160" show-overflow-tooltip align="center"></el-table-column>

      <el-table-column label="操作" align="center" width="270">
        <template slot-scope="{row}">
          <el-button class="filter-item" size="mini" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="updateHandle(row)">
            编辑
          </el-button>
          <el-button class="filter-item" size="mini" style="margin-left: 10px;" type="primary" icon="el-icon-delete" @click="deleteHandle(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageCurrent" :limit.sync="listQuery.limit" @pagination="getList" />

    <addOpen v-if="addOpenView" ref="addOpen" @closeOpen="getList"></addOpen>
  </div>
</template>

<script>
import { userList, deleteUser } from '@/api/user'
import Pagination from '@/components/Pagination'
import addOpen from './addOpen.vue'

export default {
  name: 'UserPermission',
  components: { Pagination, addOpen },
  data() {
    return {
      total: 0,
      listLoading: false,
      dataForm: {},
      listQuery: {
        pageCurrent: 1,
        limit: 10
      },
      list: [],
      addOpenView: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    updateHandle(row) {
      this.addOpenView = true
      this.$nextTick(() => {
        this.$refs.addOpen.init(row)
      })
    },
    deleteHandle(row) {
      this.$confirm(`确认删除用户名称【${row.username}】此条数据？`, '提示', {
        type: 'warning'
      }).then(_ => {
        deleteUser([
          row.userId
        ]).then((res) => {
          if(res.code == 0) {
            this.$notify({
              message: '操作成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }
        })
      }).catch(_ => {})
    },
    resetForm() {
      this.$refs.dataForm.resetFields()
      this.getList()
    },
    getList() {
      userList({
        ...this.dataForm,
        ...this.listQuery
      }).then(res => {
        this.list = res.body.list
        this.total = res.body.totalCount
      })
    },
  }
  
}
</script>

<style lang="scss" scoped>
 .app-container {
  padding: 20px;
 } 
</style>
